﻿using Msp.Scaffolding.Entities;
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Reflection;

namespace Msp.Scaffolding
{
  /*  internal class Tool
    {

        public Tool()
        {
        }


        public void Get()
        {
            var ass = Assembly.GetAssembly(typeof(BaseAddEntity));

            var types = ass.GetTypes().Where(x => x.GetCustomAttribute(typeof(FreeSql.DataAnnotations.TableAttribute)) != null);

            foreach (var item in types)
            {
                if (!item.Name.EndsWith("TenantEntity"))
                {
                    continue;
                }
                var idprop = item.GetProperty("Id");

                EntityInfo entityInfo = new EntityInfo(item.Namespace, item.Name, item.BaseType.Name, idprop.PropertyType.Name, "");

                entityInfo.ModuleName =  "NoModuleName";


                var props = item.GetProperties();

                var mattrs = item.CustomAttributes;

                foreach (var prop in props)
                {
                    var attrs = prop.CustomAttributes;

                    Entities.PropertyInfo info = new Entities.PropertyInfo(prop.PropertyType.Name, prop.Name);

                    // prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)

                    info.IsVirtual = prop.GetMethod.IsVirtual;
                    info.HasNullable = prop.PropertyType.IsGenericType && prop.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);

                    if (prop.PropertyType.Equals(typeof(string)))
                    {
                        info.HasNullable = prop.GetCustomAttribute(typeof(RequiredAttribute)) == null;
                    }


                    foreach (var att in attrs)
                    {


                        if (att.AttributeType == typeof(StringLengthAttribute))
                        {
                            var len = prop.GetCustomAttribute(typeof(StringLengthAttribute)) as StringLengthAttribute;
                            info.MinLength = len.MinimumLength;
                            info.MaxLength = len.MaximumLength;
                            info.StringLength_Msg = len.ErrorMessage;
                        }
                        else if (att.AttributeType == typeof(MaxLengthAttribute))
                        {
                            var max = prop.GetCustomAttribute(typeof(MaxLengthAttribute)) as MaxLengthAttribute;
                            info.MaxLength = max.Length;
                            info.MaxLengthMsg = max.ErrorMessage;
                        }
                        else if (att.AttributeType == typeof(DisplayAttribute))
                        {
                            var max = prop.GetCustomAttribute(typeof(DisplayAttribute)) as DisplayAttribute;
                            info.DisplayName = max.Name;
                        }
                        else if (att.AttributeType == typeof(RequiredAttribute))
                        {
                            var max = prop.GetCustomAttribute(typeof(RequiredAttribute)) as RequiredAttribute;
                            info.Required = true;
                            info.Required_msg = max.ErrorMessage;
                        }
                        else if (att.AttributeType == typeof(RegularExpressionAttribute))
                        {
                            var max = prop.GetCustomAttribute(typeof(RegularExpressionAttribute)) as RegularExpressionAttribute;
                            info.RegularExpression = max.Pattern;
                            info.RegularExpressionMsg = max.ErrorMessage;
                        }
                        else if (att.AttributeType == typeof(RangeAttribute))
                        {
                            var max = prop.GetCustomAttribute(typeof(RangeAttribute)) as RangeAttribute;
                            info.Range_Max = (int)max.Maximum;
                            info.Range_Min = (int)max.Minimum;
                            info.Range_Msg = max.ErrorMessage;
                        }
                        else if (att.AttributeType == typeof(DataTypeAttribute))
                        {
                            var max = prop.GetCustomAttribute(typeof(DataTypeAttribute)) as DataTypeAttribute;
                            info.DataType = max.DataType.ToString();
                            info.DataType_Msg = max.ErrorMessage;
                        }
                        else if (att.AttributeType == typeof(CompareAttribute))
                        {
                            var max = prop.GetCustomAttribute(typeof(CompareAttribute)) as CompareAttribute;
                            info.Compare = max.OtherProperty;
                            info.DataType_Msg = max.ErrorMessage;
                        }
                        //else if (att.AttributeType == typeof(FieldAttribute))
                        //{
                        //    var max = prop.GetCustomAttribute(typeof(FieldAttribute)) as FieldAttribute;
                        //    info.FieldInfo = max;
                        //}

                        //else if (att.AttributeType == typeof(RemoteAttribute))
                        //{
                        //    var max = prop.GetCustomAttribute(typeof(RemoteAttribute)) as RemoteAttribute;
                        //    info.Compare = max.OtherProperty;
                        //    info.DataType_Msg = max.ErrorMessage;
                        //}




                    }
                    entityInfo.Properties.Add(info);
                }
            }

        }
    }*/
}
